package com.example.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.dto.PageDto;
import com.example.common.vo.PageVo;
import com.example.common.vo.Result;
import com.example.entity.dto.CourseDto;
import com.example.entity.vo.CourseVo;
import com.example.service.pojo.Course;
import com.example.service.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Tag(name = "课程管理")
@RequestMapping("/course")
@RequiredArgsConstructor
public class CourseController {

    private final CourseService courseService;

    @GetMapping("{courseId}")
    @Operation(summary = "根据id查询课程信息")
    public Result<CourseVo> getCourseById(@PathVariable Integer courseId){
        Course course = courseService.getById(courseId);
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(course, courseVo);
        return Result.success(courseVo);
    }

    @PostMapping
    @Operation(summary = "添加课程信息")
    public Result<Boolean> addCourse(@RequestBody CourseDto courseDto){
        Course course = new Course();
        BeanUtils.copyProperties(courseDto, course);
        course.setCreateTime(new Date());
        course.setUpdateTime(new Date());
        boolean save = courseService.save(course);
        return Result.success(save);
    }

    @DeleteMapping("{courseId}")
    @Operation(summary = "根据id删除课程信息")
    public Result<Boolean> deleteCourseById(@PathVariable Integer courseId){
        boolean remove = courseService.removeById(courseId);
        return Result.success(remove);
    }

    @PutMapping
    @Operation(summary = "修改课程信息")
    public Result<Boolean> updateCourse(@RequestBody CourseDto courseDto){
        Course course = new Course();
        BeanUtils.copyProperties(courseDto, course);
        course.setUpdateTime(new Date());
        boolean update = courseService.updateById(course);
        return Result.success(update);
    }

    @PostMapping("page")
    @Operation(summary = "分页查询课程信息")
    public Result<PageVo<CourseVo>> getCoursePage(@RequestBody PageDto<CourseDto> pageDto){
        if(pageDto==null){
            return Result.clientFail("分页参数不能为空", null);
        }
        Page<Course> page = Page.of(pageDto.getPageNum(), pageDto.getPageSize());
        QueryWrapper<Course> wrapper = new QueryWrapper();
        wrapper.like("name", pageDto.getQuery().getName());
        page = courseService.page(page, wrapper);
        List<Course> records = page.getRecords();
        List<CourseVo> collect = records.stream().map(course -> {
            CourseVo courseVo = new CourseVo();
            BeanUtils.copyProperties(course, courseVo);
            return courseVo;
        }).collect(Collectors.toList());
        PageVo<CourseVo> pageVo = new PageVo<>(page.getTotal(), collect);
        return Result.success(pageVo);
    }


}
